package com.example.liuke.guangguang.aIdl;

import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.annotation.Nullable;
import android.widget.Toast;

/**
 * Created by liuke on 2018/5/17.
 */

public class MyService extends Service {
    Handler handler=new Handler();
    Runnable runnable=new Runnable() {
        @Override
        public void run() {
            // TODO Auto-generated method stub
            //要做的事情
            Toast.makeText(MyService.this,"dingshi",Toast.LENGTH_SHORT).show();
            handler.postDelayed(this, 2000);
        }
    };
    // 实例化AIDL的Stub类(Binder的子类)
    IMyAidlInterface.Stub mBinder = new IMyAidlInterface.Stub() {

        public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,
                               double aDouble, String aString) {
            System.out.println("客户端通过AIDL与远程后台成功通信");
        }


        //重写接口里定义的方法
        @Override
        public String getMessage() throws RemoteException {

            return "women ";
        }
    };

    //重写与Service生命周期的相关方法
    @Override
    public void onCreate() {
        super.onCreate();
        handler.postDelayed(runnable, 2000);
        System.out.println("执行了onCreat()");

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        System.out.println("执行了onStartCommand()");
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        System.out.println("执行了onDestory()");
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {

        System.out.println("执行了onBind()");
        //在onBind()返回继承自Binder的Stub类型的Binder，非常重要
        return mBinder;
    }

    @Override
    public boolean onUnbind(Intent intent) {
        System.out.println("执行了onUnbind()");
        return super.onUnbind(intent);
    }

}

